package com.tarena.lbs.basic.web.controller;

import com.alibaba.fastjson.JSON;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.basic.web.service.AreaService;
import com.tarena.lbs.common.basic.converter.AreaConverter;
import com.tarena.lbs.pojo.basic.bo.AreaBO;
import com.tarena.lbs.pojo.basic.vo.AreaVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/basic/")
public class AreaController {
    @Autowired
    private AreaService areaService;

    @GetMapping("area/queryList")
    public Result<List<AreaVO>> proviceList()throws BusinessException {
        List<AreaBO> bos=areaService.queryAreaList(0L);
        List<AreaVO> areaVOS = boList2VoList(bos);
        return new Result<>(areaVOS);
    }
    @GetMapping("area/{parentId}/children")
    public Result<List<AreaVO>> childrenList(@PathVariable Long parentId)throws BusinessException {
        List<AreaBO> bos=areaService.queryAreaList(parentId);
        List<AreaVO> areaVOS = boList2VoList(bos);
        return new Result<>(areaVOS);
    }
    @GetMapping("area/tree")
    public Result<List<AreaVO>> areaTree(){
        List<AreaVO> areaVOS = assembleAreaTree();
        return new Result<>(areaVOS);
    }

    /**
     * 数据转换
     * @param boList
     * @return
     */
    public List<AreaVO> boList2VoList(List<AreaBO> boList ){
        List<AreaVO> areaVoList = new ArrayList<>();
        if (CollectionUtils.isEmpty(boList)) {
            return areaVoList;
        }
        for (AreaBO areaBO : boList) {
            AreaVO areaVO = new AreaVO();
            BeanUtils.copyProperties(areaBO,areaVO);
            areaVoList.add(areaVO);
        }
        return areaVoList;
    }
    //写死的数据
    public List<AreaVO> assembleAreaTree(){
        //classpath路径 类加载器读取资源 读取流 拿到文本
        String tree="[{\n" +
                "  \"id\": 1,\n" +
                "  \"parentId\": 0,\n" +
                "  \"name\": \"北京市\",\n" +
                "  \"children\": [{\n" +
                "    \"id\": 2,\n" +
                "    \"parentId\": 1,\n" +
                "    \"name\": \"北京市市辖区\",\n" +
                "    \"children\": [{\n" +
                "      \"id\": 3,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"东城区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 4,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"西城区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 5,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"朝阳区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 6,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"丰台区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 7,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"石景山区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 8,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"海淀区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 9,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"门头沟区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 10,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"房山区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 11,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"通州区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 12,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"顺义区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 13,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"昌平区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 14,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"大兴区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 15,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"怀柔区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 16,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"平谷区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 17,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"密云区\",\n" +
                "      \"children\": null\n" +
                "    }, {\n" +
                "      \"id\": 18,\n" +
                "      \"parentId\": 2,\n" +
                "      \"name\": \"延庆区\",\n" +
                "      \"children\": null\n" +
                "    }]\n" +
                "  }]\n" +
                "}]";
        List<AreaVO> areaVOS = JSON.parseArray(tree, AreaVO.class);
        return areaVOS;
    }
}
